package com.oj.common.core.domain;

import com.oj.common.core.enums.ResultCode;
import lombok.Data;

/**
 * @Description
 * @Author bin
 * @Date 2025/4/3
 */

@Data
public class R <T>{

    private String msg;

    private int code; // 需要定义一些统一的code

    private T data; // 数据体 接口返回的数据

    public static <T> R<T> ok(){
        return assembleResult(ResultCode.SUCCESS, null);
    }

    public static <T> R<T> ok(T data){
        return assembleResult(ResultCode.SUCCESS, data);
    }

    public static <T> R<T> ok(ResultCode resultCode,T data){
        return assembleResult(resultCode, data);
    }

    public static <T> R<T> fail(){
        return assembleResult(ResultCode.FAILURE, null);
    }

    public static <T> R<T> fail(ResultCode resultCode){
        return assembleResult(resultCode, null);
    }

    public static <T> R<T> fail(int code, String msg){
        return assembleResult(code, msg, null);
    }


    public static <T> R<T> assembleResult(ResultCode resultCode, T data) {
        R<T> r = new R<>();
        r.setCode(resultCode.getCode());
        r.setMsg(resultCode.getMsg());
        r.setData(data);
        return r;
    }
    public static <T> R<T> assembleResult(int code, String msg, T data) {
        R<T> r = new R<>();
        r.setCode(code);
        r.setMsg(msg);
        r.setData(data);
        return r;
    }
}
